/*
 * Copyright 2008 Google Inc.
 * Licensed under the Apache License, Version 2.0:
 *  http://www.apache.org/licenses/LICENSE-2.0
 */
package {

import com.google.maps.Copyright;
import com.google.maps.CopyrightCollection;
import com.google.maps.LatLng;
import com.google.maps.LatLngBounds;
import com.google.maps.Map;
import com.google.maps.TileLayerBase;

import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.*;
import flash.geom.Point;
import flash.net.URLRequest;

import mx.controls.LinkButton;

public class FireTileLayer extends TileLayerBase {

    public var map:Map;
    public var nasaLabel:LinkButton;
    public var nasaText:String;

    /**
     * Constructor for the class.
     */
    public function FireTileLayer(map:Map, nasaLabel:LinkButton,
                                  nasaText:String) {
        var copyrightCollection:CopyrightCollection = new CopyrightCollection();

        // Add a custom copyright that will apply to the entire map layer.
        copyrightCollection.addCopyright(
                new Copyright("ThreeZTileProvider",
                        new LatLngBounds(new LatLng(-180, 90),
                                new LatLng(180, -90)),
                        0,
                        "©2008 Informing Design"));
        this.map = map;
        this.nasaLabel = nasaLabel;
        this.nasaText = nasaText;
        super(copyrightCollection);
    }

    /**
     * Creates and loads a tile (x, y) at the given zoom level.
     * @param tilePos  Tile coordinates.
     * @param zoom  Tile zoom.
     * @return  Display object representing the tile.
     */
    public override function loadTile(tilePos:Point,
                                      zoom:Number):DisplayObject {
        var loader:Loader = new Loader();
        var ymax:int = 1 << zoom;
        var y:int = ymax - tilePos.y - 1;

        var mapMinZoom:Number = 5;
        var mapMaxZoom:Number = 8;
        var url:String;

        if ((zoom < mapMinZoom) || (zoom > mapMaxZoom)) {
            if (nasaText == "") nasaText = nasaLabel.label;
            nasaLabel.label = "No imagery available at this zoom";
            url = "http://mapvisage.appspot.com/fires/tiles/none.png";
        } else {
            if (nasaText != "") {
                nasaLabel.label = nasaText;
            }

            var mapBounds:LatLngBounds = new LatLngBounds(
                    new LatLng(-43.9, 135.0), new LatLng(-31.0, 154.0));

            var tileBounds:LatLngBounds = new LatLngBounds(
                    map.MERCATOR_PROJECTION.fromPixelToLatLng(new Point((tilePos.x)
                            * 256, (tilePos.y + 1) * 256), zoom),
                    map.MERCATOR_PROJECTION.fromPixelToLatLng(new Point((tilePos.x
                            + 1) * 256, (tilePos.y) * 256), zoom)
                    );
            if (mapBounds.intersects(tileBounds)) {
                url = "http://mapvisage.appspot.com/zip/tiles/nasa/" + zoom
                        + "/" + tilePos.x + "/" + y + ".png";
            } else {
                url = "http://mapvisage.appspot.com/fires/tiles/none.png";
            }
        }
        var urlRequest:URLRequest = new URLRequest(url);
        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        loader.load(urlRequest);
        return loader;
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }
}
}
